# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import MySQLdb


class JingdongPipeline(object):

    def __init__(self):
        self.conn = MySQLdb.connect(host='127.0.0.1',
                                    port=3306,
                                    user='root',
                                    passwd='root',
                                    db='test',
                                    charset='utf8'
                                    )
        self.cursor = self.conn.cursor()
        self.cursor.execute("truncate table jdinfo")
        self.conn.commit()

    def process_item(self, item, spider):

        insert_cmd = "insert into jdinfo (productId, productName, productColor, orderTime, commentTime, score, " \
                     "userClientShow, userLevelName, commentContent, commentplusafterDays, commentplusContent) " \
                     "VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"

        data = (item["productId"], item["productName"], item["productColor"], item["orderTime"],
                item["commentTime"], item["score"], item["userClientShow"], item["userLevelName"],
                item["commentContent"], item["commentplusafterDays"], item["commentplusContent"])

        try:
            self.cursor.execute(insert_cmd, data)
            self.conn.commit()
        except Exception, e:
            print(Exception, e)

        return item
